package com.cqhfan.common.utils;

import com.cqhfan.common.constants.RedisConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @author 眭传洪
 * @create 2024/5/3 16:26
 * @meet 2022/02/25
 * @start 2022/6/24
 */
@Component
@RequiredArgsConstructor
public class IdUtil {

    private final StringRedisTemplate stringRedisTemplate;

    public Long getId(String prefix) {
        String key = RedisConstants.DISTRIBUTED_ID_KEY_PREFIX + prefix;
        long value = stringRedisTemplate.opsForValue().increment(key).longValue();
        return (System.currentTimeMillis() - 1001980800L) / 1000 << 31 | value;
    }

}
